import 'dart:ui' as ui;
import 'dart:async';
import 'package:flutter/material.dart';

import 'BGPhotoViewSimpleScreen.dart';

class BGExamImageWidget extends StatefulWidget {
  BGExamImageWidget({Key? key, required this.imgUrl, required this.source}) : super(key: key);
  final String? imgUrl;
  final String? source; // 1.MatchList OptionItem 100,100  2.HeadWrite大图 其它小图 3.Content 大图 4.Reference 小图
  @override
  State<BGExamImageWidget> createState() => _BGExamImageWidgetState();
}

class _BGExamImageWidgetState extends State<BGExamImageWidget> {
  @override
  Widget build(BuildContext context) {
    if (widget.imgUrl == null || widget.imgUrl == '') {
      return SizedBox();
    }
    // print(widget.imgUrl);
    Image imagePic = Image.network(widget.imgUrl!);
    Completer<ui.Image> completer = Completer<ui.Image>();
    imagePic.image.resolve(ImageConfiguration()).addListener(ImageStreamListener((ImageInfo info, bool _) {
      completer.complete(info.image);
    }));

    return FutureBuilder<ui.Image>(
      future: completer.future,
      builder: (BuildContext context, AsyncSnapshot<ui.Image> snapshot) {
        if (snapshot.hasData) {
          ui.Image? image = snapshot.data;
          if (image == null) return SizedBox();
          bool isVertical = image.width < image.height;
          bool isBig = (widget.source == 'Content' || widget.source == 'HeadWrite' || widget.source == 'HeadSpeak');
          double width = 0;
          double? height;
          if (isBig) {
            if (isVertical)
              width = 450;
            else
              width = 600;
          } else {
            if (isVertical)
              width = 80;
            else
              width = 150;
          }
          if (widget.source == 'MatchList' || widget.source == 'OptionItem') {
            width = 100;
            height = 100;
          }
          if (widget.source == 'MatchItem') {
            width = 150;
            height = 150;
          }

          return GestureDetector(
              onTap: () {
                Navigator.of(context).push(FadeRoute(page: BGPhotoViewSimpleScreen(imgUrl: widget.imgUrl!)));
              },
              child: SizedBox(width: width, height: height, child: imagePic));
        } else {
          return widget.source == 'MatchItem' ? SizedBox(width: 150, height: 150, child: imagePic) : Text('Loading...');
        }
      },
    );
  }
}

class FadeRoute extends PageRouteBuilder {
  final Widget? page;
  FadeRoute({this.page})
      : super(
          pageBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
          ) =>
              page!,
          transitionsBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
            Widget child,
          ) =>
              FadeTransition(
            opacity: animation,
            child: child,
          ),
        );
}
